<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js函数之全局函数</title>
</head>
<body>
  <ul>
    <li>使用function func() {}或var func=function() {}定义的函数都会直接挂载到window上，成为全局函数</li>
    <li>全局函数可能与window上已有成员冲突，造成全局污染</li>
    <li>建议不要独立定义函数，而是将函数放在对象或类的内部去定义</li>
  </ul>
  <script>
    console.log(window.screenX)
    var screenX = function() {
      console.log('获取X轴位置')
    }
    console.log(window.screenX)  // 覆盖window上已有成员，造成全局污染
    const position = {
      screenX() {
        console.log('获取X轴位置')
      }
    }
    console.log(position.screenX)
  </script>
</body>
</html>